package com.tanhua.appserver.controller;

import com.tanhua.appserver.service.MovementsService;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;
    @PostMapping
    public ResponseEntity movements(Movement movement , MultipartFile[] imageContent) throws IOException {

        movementsService.publish(movement,imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询个人动态
     * @param page
     * @param size
     * @param userId
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity getOwnMovements(@RequestParam(defaultValue = "1") int page,
                                          @RequestParam(defaultValue = "10") int size,
                                          @RequestParam Long userId){

        PageResult pr =  movementsService.getOwnMovements(page,size,userId);
        return ResponseEntity.ok(pr);

    }

    /**
     * 查询好友动态
     * @param page
     * @param size
     * @return
     */
    @GetMapping
    public ResponseEntity getFriendsMovements(@RequestParam(defaultValue = "1") int page,
                                              @RequestParam(defaultValue = "10") int size){

        PageResult pr =movementsService.getFriendsMovements(page,size);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查询推荐动态
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity getRecommendMovements(@RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10") int size){

        PageResult pr =movementsService.getRecommendMovements(page,size);
        return ResponseEntity.ok(pr);

    }

    /**
     * 通过动态id查询单条动态数据
     * @param movementID 动态id
     * @return 返回封装的动态VO对象
     */
    @GetMapping("/{id}")
    public ResponseEntity getMovementByID(@PathVariable("id") String movementID){

        MovementsVo vo =movementsService.getMovementByID(movementID);
        return ResponseEntity.ok(vo);
    }
    /**
     * 点击点赞按钮进来
     * @param id
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String id){
        Integer counts =movementsService.like(id);
        return ResponseEntity.ok(counts);
    }

    /**
     * 点击点赞按钮进来
     * @param id
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity disLike(@PathVariable("id") String id){

        Integer counts =movementsService.disLike(id);
        return ResponseEntity.ok(counts);

    }
    /**
     * 点击点赞按钮进来
     * @param id
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String id){
        Integer counts =movementsService.love(id);
        return ResponseEntity.ok(counts);
    }
    /**
     * 点击点赞按钮进来
     * @param id
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String id){

        Integer counts =movementsService.unlove(id);
        return ResponseEntity.ok(counts);

    }
}